<?php
namespace app\admin\controller;
use app\admin\Validate\Stu as Vstu;
use app\admin\model\Numbr as Nu;
use think\Db;
use think\Request;
Class Numbr extends Common
{
    public function index()
    {
        $re = new Nu();
        $list = $re->index();
        $count = $re->count();
        //halt($list);
        $page = $list->render();
        return $this->fetch('index',['res'=>$list,'count'=>$count,'page'=>$page]);
    }
    public function create()
    {
        $stu = new Nu();
        if(request()->isPost()){
           /* $vstu = new Vstu;
            if(!$vstu->check($_POST)){
                $this->error($vstu->getError());
                die();
            }*/
            $adds = request()->file('n_photo');
            if($adds){
                $result = $stu->adds();
            }else{
                $result= $stu->add();
            }
            if($result){
                $this->success('添加成功','admin/numbr/index');
            }else{
                $this->error('添加失败','admin/numbr/create');
            }
        }else{
            return $this->fetch('create');
        }

    }
    public function update()
    {
        $stu = new Nu();
        $id=input('param.n_id');
        //halt($id);
        if(request()->isPost()){
            /*$vstu = new Vstu;
            if(!$vstu->check($_POST)){
                $this->error($vstu->getError());
                die();
            }*/
            $file = request()->file('n_photo');
            //halt($file);
           if($file){
                define('ROOT',dirname(dirname(dirname(dirname(__FILE__)))));
                $post = Db::table('lou_number')->where('n_id',$id)->find();
                $amp = $post['n_photo'];
                $urle = ROOT."/public/uploads/$amp";
                if(file_exists($urle))
                {
                    @unlink($urle);
                }
                $result = $stu->edits();
            }else{
                $result= $stu->edit();
            }
            if($result){
                $this->success('修改成功','admin/numbr/index');
            }else{
                $this->error('修改失败','admin/numbr/update');
            }
        }else{
            $data = Nu::find($id);
            //halt($data);
            return $this->fetch('update',['res'=>$data]);
        }
    }
    public function delete($id)
    {
        $stu =new Nu();
        $result = $stu->del($id);
        if($result){
            $this->success('删除成功','admin/numbr/index');
        }else{
            $this->error('删除失败','admin/numbr/index');
        }

    }

}